﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="js-console.css" rel="stylesheet" />
	<!-- 11. Write a function that formats a string using placeholders: 
			var str = stringFormat("Hello {0}!","Peter"); //str = "Hello Peter!";

		The function should work with up to 30 placeholders and all types

			var format = "{0}, {1}, {0} text {2}";
			var str = stringFormat(format,1,"Pesho","Gosho");
			//str = "1, Pesho, 1 text Gosho" -->
</head>
<body>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		function dynamicReplace(str, i, value) {
			var expr = '\\{' + i + '\\}';
			var re = new RegExp(expr, 'g');
			return str.replace(re, value);
		}

		function stringFormat(format) {
			var result = new String(format);

			if (arguments.length > 1) {
				for (var i = 1; i < arguments.length; i++) {
					result = dynamicReplace(result, i - 1, arguments[i]);
				}
			}

			return result;
		}

		//var str = stringFormat("Hello {0}!", "Peter"); //str = "Hello Peter!";
		var format = "{0}, {1}, {0} text {2}";
		var str = stringFormat(format, 1, "Pesho", "Gosho");

		jsConsole.writeLine("Format string is: " + format);
		jsConsole.writeLine("Result string is: " + str);
	</script>
</body>
</html>
